package com.lyy.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyy.Vo.ProductVo;
import com.lyy.aspect.ControllerEndpoint;
import com.lyy.response.Result;
import com.lyy.system.entity.Product;
import com.lyy.system.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.TreeMap;

@RestController
@RequestMapping("/product")
@Api(value = "业务管理模块",tags = "物资资料接口")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ControllerEndpoint(exceptionMessage = "添加物资资料失败", operation = "添加物资资料")
    @ApiOperation(value = "添加物资资料",notes = "添加物资资料信息")
    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody Product product){
        productService.addProduct(product);
        return Result.ok();
    }

    @PostMapping("/queryProuctList")
    @ApiOperation(value = "条件分页查询物资列表",notes = "物资列表查询")
    public Result queryProuctList(@RequestParam(required = true,defaultValue = "1") Integer current,
                                  @RequestParam(required = true,defaultValue = "6") Integer size,
                                  @RequestBody ProductVo productVo){
        Page<Product> page =new Page<>(current,size);
        QueryWrapper<Product> wrapper =getWrapper(productVo);
        IPage<Product> productIPage =productService.queryProuctList(page,wrapper);
        Long total=productIPage.getTotal();
        List<Product> productList =productIPage.getRecords();
        return Result.ok().data("total",total).data("productList",productList);
    }

    @ControllerEndpoint(exceptionMessage = "删除物资失败", operation = "删除物资")
    @ApiOperation(value = "删除物资",notes = "删除物资资料")
    @DeleteMapping("/deleteProduct")
    public Result deleteProduct(Long id){
        productService.deleteProduct(id);
        return Result.ok();
    }

    @PostMapping("/productDetail")
    @ApiOperation(value = "物资资料详情",notes = "物资资料详情")
    public Result productDetail(Long id){
        Product productDetail= productService.productDetail(id);
        return Result.ok().data("productDetail",productDetail);
    }

    @ControllerEndpoint(exceptionMessage = "更新物资资料失败", operation = "更新物资资料")
    @ApiOperation(value = "更新物资资料",notes = "更新物资资料信息")
    @PostMapping("/updateProduct")
    public Result updateProduct(@RequestBody Product product){
        productService.updateProduct(product);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "物资移入回收站失败", operation = "物资移入回收站")
    @ApiOperation(value = "物资移入回收站",notes = "物资移入回收站")
    @PostMapping("/remove")
    public Result remove(Long id){
        productService.remove(id);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "物资从回收站恢复失败", operation = "物资从回收站恢复")
    @ApiOperation(value = "物资从回收站恢复",notes = "物资从回收站恢复")
    @PostMapping("/back")
    public Result back(Long id){
        productService.back(id);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "物资审核失败", operation = "物资审核")
    @ApiOperation(value = "物资审核",notes = "物资审核")
    @PostMapping("/publish")
    public Result publish(Long id){ 
        productService.publish(id);
        return Result.ok();
    }

    /**
     * 封装查询条件
     * @param productVo
     * @return
     */
    private QueryWrapper<Product> getWrapper(ProductVo productVo){
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        if (productVo!=null){
            if (!StringUtils.isEmpty(productVo.getName())){
                wrapper.eq("name",productVo.getName());
            }
            if (!StringUtils.isEmpty(productVo.getStatus())){
                wrapper.eq("status",productVo.getStatus());
            }
            if (!StringUtils.isEmpty(productVo.getThreeCategoryId())){
                wrapper.eq("three_category_id",productVo.getThreeCategoryId());
            }else if (!StringUtils.isEmpty(productVo.getTwoCategoryId())){
                wrapper.eq("two_category_id",productVo.getTwoCategoryId());
            }else if (!StringUtils.isEmpty(productVo.getOneCategoryId())){
                wrapper.eq("one_category_id",productVo.getOneCategoryId());
            }
        }
        return wrapper;
    }
}
